package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.CommentAdapter;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.Comment;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.json.CommentJson;
import com.hcinfos.gsoo.util.LogUtil;

public class CommentListActivity extends Activity implements OnClickListener {
	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;

	private ListView listView;
	private ImageView right_img;
	private CommentAdapter adapter;

	private RelativeLayout progress_layout;
	private LinearLayout loading_dialog_content_layout;
	private RelativeLayout loading_staus_rl;
	private TextView status_tv;

	private final int GET_COMMENT_SUCCESS = 0x1120;
	private final int GET_COMMENT_FAIL = GET_COMMENT_SUCCESS + 1;
	private final int GET_COMMENT_EMPTY = GET_COMMENT_SUCCESS + 2;
	private List<Comment> commentList;

	private View moreView;
	private ProgressBar pageBar;
	private TextView loading_tv;
	private RelativeLayout load_rl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		commentList = new ArrayList<Comment>();
		setContentView(R.layout.activity_comment_list);
		initRes();
	}

	private void initRes() {
		progress_layout = (RelativeLayout) findViewById(R.id.progress_layout);
		progress_layout.setOnClickListener(this);
		progress_layout.setVisibility(View.VISIBLE);
		loading_dialog_content_layout = (LinearLayout) findViewById(R.id.loading_dialog_content_layout);
		loading_staus_rl = (RelativeLayout) findViewById(R.id.loading_staus_rl);
		loading_staus_rl.setVisibility(View.GONE);// 内容提示
		loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条

		status_tv = (TextView) findViewById(R.id.status_tv);
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		left_ib.setOnClickListener(this);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("点评列表");
		right_img = (ImageView) findViewById(R.id.right_img);
		right_img.setImageResource(R.drawable.add_comment_normal);
		right_img.setVisibility(View.VISIBLE);
		right_img.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.listView);
		listView.setVisibility(View.GONE);

		adapter = new CommentAdapter(this, commentList);
		moreView = getLayoutInflater().inflate(R.layout.loader_more, null);
		loading_tv = (TextView) moreView.findViewById(R.id.load_tv);
		load_rl = (RelativeLayout) moreView.findViewById(R.id.load_rl);
		load_rl.setOnClickListener(this);
		pageBar = (ProgressBar) moreView.findViewById(R.id.pb);
		listView.addFooterView(moreView);
		listView.setAdapter(adapter);

		detail = (ViewSuppDetail) getIntent().getSerializableExtra("detail");
		if (detail == null) {
			status_tv.setText("加载错误");
			progress_layout.setClickable(false);
		} else {
			progress_layout.setClickable(true);
			shopId = detail.getId();
			LogUtil.d("wxf", "shopId=" + shopId);
			obtainCommentList();
		}
	}

	private ViewSuppDetail detail;

	@Override
	protected void onResume() {

		super.onResume();
	}

	private String shopId;
	private int page = 1;
	private int rows = 6;
	private int total = 0;

	private void obtainCommentList() {

		new Thread(new Runnable() {

			@Override
			public void run() {

				try {
					String result = HttpClientUtil.getFromWebByHttpClient(
							CommentListActivity.this, Urls.SHOP_COMMENT_LIST
									+ "record.suppId=" + shopId + "&page="
									+ page + "&rows=" + rows
									+ "&sort=update_datetime");
					LogUtil.d("wxf", "result:" + result);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = GET_COMMENT_FAIL;
						handler.sendMessage(msg);
					} else {
						List<Comment> comments = CommentJson
								.getCommentListByJson(result);
						LogUtil.d("wxf", "comments:" + comments.size());
						if (comments.size() == 0) {
							Message msg = handler.obtainMessage();
							msg.what = GET_COMMENT_EMPTY;
							handler.sendMessage(msg);
						} else {
							total = CommentJson.total;
							commentList.addAll(comments);
							Message msg = handler.obtainMessage();
							msg.what = GET_COMMENT_SUCCESS;
							handler.sendMessage(msg);
						}

					}
				} catch (Exception e) {
					LogUtil.d("wxf", "e:" + e.toString());
					Message msg = handler.obtainMessage();
					msg.what = GET_COMMENT_FAIL;
					handler.sendMessage(msg);
				}
			}
		}).start();
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case GET_COMMENT_FAIL:
				status_tv.setText("加载失败,点击我重新加载");
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				break;
			case GET_COMMENT_SUCCESS:

				progress_layout.setVisibility(View.GONE);
				listView.setVisibility(View.VISIBLE);
				adapter.setDataChange(commentList);
				if (total > rows && total != commentList.size()) {
					loading_tv.setVisibility(View.VISIBLE);
					pageBar.setVisibility(View.GONE);
				} else {
					listView.removeFooterView(moreView);
				}
				break;
			case GET_COMMENT_EMPTY:
				status_tv.setText("暂无评论数据");
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				progress_layout.setClickable(false);
				break;
			}
		};
	};

	private Intent intent;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.right_img:
			intent = new Intent(CommentListActivity.this, CommentActivity.class);
			intent.putExtra("detail", detail);
			startActivity(intent);
			finish();
			break;
		case R.id.load_rl:
			page += 1;
			pageBar.setVisibility(View.VISIBLE);
			obtainCommentList();
			break;
		case R.id.progress_layout:
			loading_staus_rl.setVisibility(View.GONE);// 内容提示
			loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
			obtainCommentList();
			break;
		}
	}
}
